import sys
import json
from PyQt6.QtWidgets import QApplication, QMainWindow, QStackedWidget, QWidget, QVBoxLayout, QLabel
from PyQt6.QtGui import QFont, QAction
from PyQt6.QtCore import QTimer
from views import DataFrame, ParaFrame, CalibrationFrame, AboutFrame, StatusFrame, SettingFrame, EventFrame, ControlFrame, CurveFrame

class MainPage(QMainWindow):
    def __init__(self):
        super().__init__()
        self.config = self.load_config()
        self.setWindowTitle("安全电设备管理系统")
        self.setGeometry(100, 100, 1024, 600)

        self.central_widget = QStackedWidget()
        self.setCentralWidget(self.central_widget)

        self.frames = {
            "data": DataFrame(self),#运行数据
            "para": ParaFrame(self),#参数设置
            # "calibration": CalibrationFrame(),
            "about": AboutFrame(self),#关于
            "status": StatusFrame(self),    #状态显示
            "control": ControlFrame(self),  #控制设置
            "event": EventFrame(self),            
            "curves": CurveFrame(self),
            "setting": SettingFrame(self)
        }

        for frame in self.frames.values():
            self.central_widget.addWidget(frame)

        self.create_menu()
        self.show_status_bar()

    
    def load_config(self):
        try:
            with open('config.bin', "r", encoding='utf-8') as f:
                return json.load(f)
        except FileNotFoundError:
            config = {'com': "COM1", 'baudrate': 115200, 'timeout': 3}
            with open('config.bin', "w", encoding='utf-8') as f:
                json.dump(config, f)
            return config

    def create_menu(self):
        menubar = self.menuBar()
        
        font = QFont()
        font.setPointSize(16)  # 设置字体大小为12
        # font.setBold(True)  # 设置字体加粗
        menubar.setFont(font)
        menu_dic = {
            "运行数据": "data",
            "参数设置": "para",
            "状态显示": "status",
            "控制命令": "control",
            "事件记录": "event",
            "定点曲线": "curves",
            "系统设置": "setting",
            "关于": "about"
        }
        def add_action(name, frame_name):
            action = QAction(name, self)
            action.triggered.connect(lambda: self.show_frame(frame_name))
            menubar.addAction(action)
        for name, frame_name in menu_dic.items():
            add_action(name, frame_name)

        exit_menu = QAction("退出", self)
        exit_menu.triggered.connect(self.close)
        menubar.addAction(exit_menu)

    def show_frame(self, frame_name):
        frame = self.frames.get(frame_name)
        if frame:
            self.central_widget.setCurrentWidget(frame)
            # if frame_name in ["para", "calibration"]:
            #     frame.read_para()
    
    def show_status_bar(self):
        #增加状态栏，分割成三部分
        self.link_status = QLabel()
        #设置最小宽度
        self.link_status.setMinimumWidth(100)
        self.server_status = QLabel()
        self.server_status.setMinimumWidth(100)
        self.notice_status = QLabel()
        self.notice_status.setMinimumWidth(100)
        self.statusBar().addWidget(QLabel("连接状态："))
        self.statusBar().addWidget(self.link_status)
        self.statusBar().addWidget(QLabel("服务器状态：")) 
        self.statusBar().addWidget(self.server_status)
        self.statusBar().addWidget(QLabel("消息通知："))
        self.statusBar().addWidget(self.notice_status)
        # self.statusBar().showMessage("欢迎使用安全电设备管理系统")
    def set_notice(self,notice,color="black"):
        self.notice_status.setText(notice)
        self.notice_status.setStyleSheet(f"color:{color}")
        self.timer=QTimer()
        self.timer.timeout.connect(lambda:self.notice_status.setText(""))
        self.timer.start(5000)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_page = MainPage()
    main_page.show()
    sys.exit(app.exec())
